﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Extensions.DependencyInjection;
using static System.Net.Mime.MediaTypeNames;

using System.IO;
using zy_simu.Data;
using Microsoft.AspNetCore.Components.WebView.Wpf;

namespace zy_simu
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
        // 创建配置类
        public SimuCfg emu_cfg = new SimuCfg();
        private BlazorWebViewReaction blazor_webview = new();
        public MainWindow()
		{
			InitializeComponent();
            this.Title = "温控设备模拟器";
            if(!Directory.Exists(@".\app"))
            {
                Directory.CreateDirectory(@".\app");
            }
            // 加载配置数据
            if (File.Exists(@".\app\zq_simu.config"))
            {
                string jsonString = File.ReadAllText(@".\app\zq_simu.config");
                emu_cfg = JsonSerializer.Deserialize<SimuCfg>(jsonString)!;
            }
            // 加载设备仿真快照模块
            DeviceSnap DevSnap = new DeviceSnap();
            // 加载测试宏模块
            MacroService macro_service = new MacroService();

            var serviceCollection = new ServiceCollection();
            serviceCollection.AddWpfBlazorWebView();
            // Kg Modify: 注册 BootstrapBlazor 服务
            serviceCollection.AddBootstrapBlazor();
            // 注册配置服务，注入blazor
            serviceCollection.AddSingleton<SimuCfg>(emu_cfg);
            serviceCollection.AddSingleton<DeviceSnap>(DevSnap);
            serviceCollection.AddSingleton<MacroService>(macro_service);
            //blazor_webview.web_view2 = this.webView.WebView;
            blazor_webview.get_webview = GetWebViewHandle;
            serviceCollection.AddSingleton<BlazorWebViewReaction>(blazor_webview);
            Resources.Add("services", serviceCollection.BuildServiceProvider());
            // 增加窗口关闭中处理
            this.Closed += CloseAll;
        }
        private Microsoft.Web.WebView2.Core.CoreWebView2? GetWebViewHandle()
        {
            return this.webView.WebView?.CoreWebView2;
        }
        private void CloseAll(object? sender, EventArgs e)
        {
            if(blazor_webview.Close != null)
                blazor_webview.Close();

            //this.webView.WebView.ExecuteScriptAsync
            //this.webView.WebView

            //if(simu_ctrl.timer != null)
            //{
            //    simu_ctrl.timer.Stop();
            //    simu_ctrl.timer.Close();
            //    simu_ctrl.timer = null;
            //}
            //simu_ctrl.Cat1Switch(0); // 关闭cat1模拟器
            //emu_cfg.pow = false; // 模拟关闭电源
            //simu_ctrl.McuSwitch(0); // 关闭mcu模拟器

            //emu_dbg.timer.Stop();
            //emu_dbg.Cat1Switch(false); // 关闭Cat1模拟器
            //emu_dbg.McuSwitch(false); // 关闭mcu模拟器
            //emu_dbg.isStarted = false;
            string jsonString = JsonSerializer.Serialize(emu_cfg);
            File.WriteAllText(@".\app\zq_simu.config", jsonString);

            //throw new NotImplementedException();
            //e.Cancel = false;
        }
    }
}
